//
//  RecreationViewCell.m
//  Happy to chat
//
//  Created by lanou3g on 15/12/8.
//  Copyright © 2015年 LL. All rights reserved.
//

#import "RecreationViewCell.h"
@import AVFoundation;
#import "RecreationModel.h"
#import "RecreationManager.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "UMSocial.h"
@interface RecreationViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *backImageview;
- (IBAction)playButton:(UIButton *)sender;
//点击事件
@property (weak, nonatomic) IBOutlet UIButton *playButton;

//单利
@property (nonatomic, strong) RecreationManager *recreationManager;

@property (nonatomic, strong) RecreationModel *model;
//视频图片
@property (nonatomic, strong) UIImageView *mImageView;
//第三方
@property (nonatomic, strong) MBProgressHUD *HUD;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//详情
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
//时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;


@end
@implementation RecreationViewCell
-(void)setupViewsWithModel:(RecreationModel *)movie{
    //初始化
    self.HUD = [MBProgressHUD new];
    //设置颜色
    [self.HUD setColor:[UIColor clearColor]];
    //添加视图
    [self.playButton addSubview:self.HUD];
    //初始化模型
    self.model = [RecreationModel new];
    self.model = movie;
    self.recreationManager = [RecreationManager defaultRecraetionManager];
    //赋值
    self.titleLabel.text =  movie.title;
    self.detailLabel.text = movie.des;
    //判断电影的时常
    if (movie.length < 60) {
        self.timeLabel.text = [NSString stringWithFormat:@"00:%ld",movie.length];
    }else{
        NSInteger a = movie.length / 60;
        int b = movie.length % 60;
        NSString *string = [NSString stringWithFormat:@"%02ld:%02d",(long)a,b];
        self.timeLabel.text = string;
    }
    //设置button的样式
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置大小
    self.mImageView =[[ UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.backImageview.frame.size.width, self.backImageview.frame.size.height-80)];
    //解析图片
    [self.mImageView sd_setImageWithURL:[NSURL URLWithString:movie.cover]];
    //添加返回的视频
    [self.backImageview addSubview:self.mImageView];
    [self.mImageView setUserInteractionEnabled:YES];
    
}
- (IBAction)playButton:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@""]) {
        [self.recreationManager playerPause];
        //添加样式
        [sender setBackgroundImage:[UIImage imageNamed:@"video_play_medium"] forState:UIControlStateNormal];
        self.playButton.titleLabel.text = @"playButton";
    }else {
        self.mImageView.image = [UIImage imageNamed:@"beijing.jpg"];
        [self.HUD show:YES];
        self.button = sender;
        [self.recreationManager shiFang];
        [self.recreationManager setupAVPlayerWithModel:self.model];
        [self setupPlayer];
        sender.titleLabel.text = @"";
        [self.playButton setBackgroundImage:nil forState:UIControlStateNormal];
        [self.recreationManager playerPlay];
        //移除小菊花的方法
        [self performSelector:@selector(removeHUB) withObject:nil afterDelay:2];
    }
    
}

/**
 *  移除
 */
- (void)removeHUB {
    [self.HUD removeFromSuperview];
}

/**
 设置播放器
 */
- (void)setupPlayer {
    self.recreationManager.avPlayerlayer.frame = self.mImageView.bounds;
    
    [self.mImageView.layer addSublayer:self.recreationManager.avPlayerlayer];
}

- (IBAction)Action4Share:(UIButton *)sender {
    
    NSLog(@"点击我了");
    if ([_delegate respondsToSelector:@selector(share)]) {
        [_delegate share];
    }
                                
                                //可以根据回调提示用户。
    
                                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                    message:nil
                                                                                   delegate:self
                                                                          cancelButtonTitle:@"OK"
                                                                          otherButtonTitles:nil, nil];
                                    [alert show];
                                }

- (IBAction)Action4Collection:(UIButton *)sender {
    
    NSLog(@"收藏了 ");
    
    
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
